﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class background : MonoBehaviour {

    // Use this for initialization
    private void Awake()
    {
        #region image充满整个屏幕
        //int width = Screen.width;
        //int height = Screen.height;
        //int designWidth = 960;//开发时分辨率宽
        //int designHeight = 640;//开发时分辨率高
        //float s1 = (float)designWidth / designHeight;
        //float s2 = (float)width / height;
        //if (s1 < s2)
        //{
        //    designWidth = Mathf.FloorToInt(designHeight * s2);
        //}
        //else if (s1 > s2)
        //{
        //    designHeight = Mathf.FloorToInt(designWidth / s2);
        //}
        //float contentScale = designWidth / (float)width;
        //RectTransform rectTransform = transform as RectTransform;
        //if (rectTransform != null)
        //{
        //    rectTransform.sizeDelta = new Vector2(designWidth, designHeight);
        //}
        #endregion

        #region SpriteRenderer充满整个屏幕
        SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();

        float cameraHeight = Camera.main.orthographicSize * 2;
        Vector2 cameraSize = new Vector2(Camera.main.aspect * cameraHeight, cameraHeight);
        Vector2 spriteSize = spriteRenderer.sprite.bounds.size;

        Vector2 scale = transform.localScale;
        if (cameraSize.x >= cameraSize.y)
        {
            scale *= cameraSize.x / spriteSize.x;
        }
        else
        {
            scale *= cameraSize.y / spriteSize.y;
        }
        transform.position = Vector2.zero;
        transform.localScale = scale;
        #endregion
    }
}
